//
//  WHView.m
//  VoiceNavigation
//
//  Created by dengweihao on 15/7/3.
//  Copyright (c) 2015年 vcyber. All rights reserved.
//

#import "WHView.h"
#import "WHLabel.h"
#import "YYKit.h"

@implementation WHView

+ (void)disMisAlert {
    UIWindow *lastWindow = [UIApplication sharedApplication].windows.lastObject;
    if (lastWindow.windowLevel == UIWindowLevelAlert + 1) {
        [lastWindow.rootViewController dismissViewControllerAnimated:YES completion:nil];
    }
}

+ (void)showAlertwithTitle:(NSString *)title message:(NSString *)message okTitle:(NSString *)okTitle okAction:(void (^)(UIAlertAction *action))okHandle cacelTitle:(NSString *)cancelTitle cancelAction:(void (^)(UIAlertAction *action))cancleHandle {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    // 修改message
    NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:message attributes:@{NSForegroundColorAttributeName:CFontTitleBlackColor, NSFontAttributeName:SYSTEMFONT(kRealwidth(15))}];
    [alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];
    
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:okTitle style:UIAlertActionStyleDefault handler:okHandle];
    [OKAction setValue:CMDarkOrangeColor forKey:@"_titleTextColor"];
    [alertController addAction:OKAction];
    
    if (cancelTitle) {
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:cancleHandle];
        [cancelAction setValue:CFontTitleBlackColor forKey:@"_titleTextColor"];
        [alertController addAction:cancelAction];
    }
    
//    UIWindow *lastWindow = [UIApplication sharedApplication].windows.lastObject;
//    if (lastWindow.windowLevel != UIWindowLevelAlert + 1) {
        UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        alertWindow.rootViewController = [[UIViewController alloc] init];
        alertWindow.windowLevel = UIWindowLevelAlert + 1;
        [alertWindow makeKeyAndVisible];
        [alertWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
//    }
    
    
}

+ (void)showAlertwithTitle:(NSString *)title message:(NSString *)message okAction:(void (^)(UIAlertAction *action))okHandle cancelAction:(void (^)(UIAlertAction *action))cancleHandle {
    [self showAlertwithTitle:title message:message okTitle:@"确定" okAction:okHandle cacelTitle:@"取消" cancelAction:cancleHandle];
}

+ (void)showAlertwithTitle:(NSString *)title message:(NSString *)message {
    [self showAlertwithTitle:title message:message okTitle:@"确定" okAction:nil cacelTitle:nil cancelAction:nil];
}

+ (void)showHudMsgAtCenter:(NSString *)message {
    [self showMessage:message inView:nil position:[NSValue valueWithCGPoint:CGPointMake(KScreenWidth/2, KScreenHeight/2)]];
}

+ (void)showHudMsg:(NSString *)message {
    [self showMessage:message inView:nil];
}

+ (void)showMessage:(NSString *)message inView:(UIView *)view {
    WHView *messageView = [self setUpWHView:message inView:view];
    [messageView fadeInAndOut];
}

+ (void)showMessage:(NSString *)message inView:(UIView *)view position:(id)point {
    WHView *messageView = [self setUpWHView:message inView:view];
    
    [messageView fadeInAndOut];
    
    if (point != ToastPositionDefault) {
        messageView.center = [point CGPointValue];
    }
}

+ (WHView *)setUpWHView:(NSString *)message inView:(UIView *)view {
    WHView *messageView = [[WHView alloc] initWithCenter:CGPointMake(KScreenWidth/2, KScreenHeight-kRealwidth(100)) Text:message Font:kRealwidth(15) color:[UIColor whiteColor]];
    
    if (view == nil) {
//        [UIApplication sharedApplication].keyWindow
        [[UIApplication sharedApplication].delegate.window addSubview:messageView];
        [[UIApplication sharedApplication].delegate.window bringSubviewToFront:messageView];
    } else {
        [view addSubview:messageView];
        [view bringSubviewToFront:messageView];
    }
    
    return messageView;
}

+ (WHView *)viewWithFrame:(CGRect)frame image:(NSString *)image {
    WHView *myView = [[WHView alloc] init];
    myView.frame = frame;
    [myView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:image]]];
    
    return myView;
}

/** 返回一个自定义的view,view的大小根据文字动态调整 */
- (WHView *)initWithCenter:(CGPoint)center Text:(NSString *)text Font:(CGFloat)font color:(UIColor *)color {
    WHView *myView = [[WHView alloc] init];
    myView.frame = CGRectMake(0, 0, 160, 30);
    myView.backgroundColor = HexColor(@"a3becccc");
    
    WHLabel *myLabel = [WHLabel labelWithFrame:CGRectMake(0, 0, 160, 30) Text:text Font:font color:color];
    myLabel.textAlignment = NSTextAlignmentCenter;
    
    myView.myLabel = myLabel;
    
    CGSize myLabelSize = myLabel.frame.size;
    CGFloat myWidth = myLabelSize.width+kRealwidth(30);
    CGFloat myHeight = kRealwidth(50); // 控制高度
    myView.clipsToBounds = YES;
    myView.layer.cornerRadius = kRealwidth(50)/2;
    CGSize myViewSize = CGSizeMake(myWidth, myHeight);
    
    CGPoint myViewPoint = myView.frame.origin;
    myView.frame = CGRectMake(myViewPoint.x, myViewPoint.y, myViewSize.width, myViewSize.height);
    myView.center = center;
    
    myLabel.center = CGPointMake(myViewSize.width/2, myViewSize.height/2);
    [myView addSubview:myLabel];
    
    return myView;
}

- (void)fadeInAndOut {
//    DLog(@"_________视图淡入_________");
//    view.transform = CGAffineTransformMakeScale(0, 0);
    self.alpha = 0;
    [UIView animateWithDuration:.5f animations:^{
        self.alpha = 1;
    } completion:^(BOOL finished) {
        [self performSelector:@selector(fadeOut) withObject:nil afterDelay:.5f];
    }];
}

- (void)fadeOut {
//    DLog(@"_________视图淡出_________");
    [UIView animateWithDuration:.5f animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

@end
